﻿
'use timer3 to reduce thread cost
Public Class tcpServer2
    Inherits tcpServerAngel
    Public Shared ReadOnly defaultThreadCount As Int64 = timerSet.defaultThreadCount
    'Private timer3Index As Guid = Guid.Empty
    'Private removed As Boolean = True
    Private timer3 As timerSet = Nothing

    Public Property threadCount() As Int64
        Get
            Return timer3.threadCount()
        End Get
        Set(ByVal value As Int64)
            timer3.threadCount = value
        End Set
    End Property

    Protected Overrides Sub startListen()
        If Not timer3 Is Nothing Then
            timer3.stop()
        End If
        timer3 = New timerSet()
        MyBase.startListen()
    End Sub

    Protected Overloads Sub handlecall(ByRef obj As Object)
        Dim handle As talkHandle = Nothing
        handle = convertor.cast(Of talkHandle)(obj)
        assert(Not handle Is Nothing, "should not call tcpServer2.handlecall without a talkHandle2.")
        'we are not safe here, if the tcp connection is not stable,
        'it will cost much time to receive, and block current thread.
        'but seems it works well on intranet now.
        If extSignal() OrElse handle.closed() OrElse _
           ticksToMilliseconds(Now.Ticks) - handle._blanktime > livetime Then
            If Not handle.removed Then
                While handle.timer3Index = Guid.Empty
                    sleep(16)
                End While
                assert(timer3.removecall(handle.timer3Index), _
                       "timer3.removecall(handle.timer3Index) returns false. timer3Index = " + _
                       Convert.ToString(handle.timer3Index))
                handle.timer3Index = Guid.Empty
            End If
            handle.release()
        Else
            'should not waste time, tcpServer2 uses thread pool
            While MyBase.handlecall(handle)
            End While
        End If
    End Sub

    Protected Overrides Sub starthandle(ByVal obj As tcpServer.talkHandle)
        Static index As Int64 = 0
        Dim handle As talkHandle = Nothing
        handle = convertor.cast(Of talkHandle)(obj)

        handle.removed = False
        handle.timer3Index = timer3.addcall(AddressOf handlecall, defaultWaitTime, handle, index)
        index += 1
        If index = threadCount() Then
            index = 0
        End If
        assert(handle.timer3Index <> Guid.Empty, "cannot add handle to timer3 call.")
    End Sub

    Protected Overrides Function generatehandleInstance() As tcpServer.talkHandle
        Return New talkHandle()
    End Function

    Public Overrides Sub [stop](Optional ByVal waittime As Int64 = maxInt64)
        If active() Then
            MyBase.[stop](waittime)
            If Not timer3 Is Nothing Then
                timer3.stop()
                timer3 = Nothing
            End If
        End If
    End Sub

    Public Shadows Class talkHandle
        Inherits tcpServer.talkHandle

        Protected Friend timer3Index As Guid
        Protected Friend removed As Boolean

        Private Sub initial2()
            timer3Index = Guid.Empty
            removed = True
        End Sub

        Public Overrides Sub release()
            MyBase.release()
            initial2()
        End Sub

        Public Overrides Sub initial()
            MyBase.initial()
            initial2()
        End Sub
    End Class
End Class
